package me.firstapp.repository.zerone.cqssc.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.zerone.cqssc.CqsscLotteryResult;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.zerone.cqssc.CqsscLotteryResultRepository;

@Repository
public class LotteryResultRepositoryImpl extends EntityRepositoryImpl<CqsscLotteryResult>
		implements CqsscLotteryResultRepository {

	@Override
	protected Class<CqsscLotteryResult> getPersistentClass() {
		return CqsscLotteryResult.class;
	}

	@Override
	public Page<CqsscLotteryResult> findPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		StringBuilder sql = new StringBuilder(" FROM CqsscLotteryResult bean WHERE 1=1");
		Map<String, Object> parameters = new HashMap<>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					sql.append(" AND bean." + key + " LIKE :value ");
					parameters.put("value", "%" + value + "%");
				}
			}
		}

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(CqsscLotteryResult.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public CqsscLotteryResult findByIssueNo(String issueNo) {
		StringBuilder sql = new StringBuilder(" FROM CqsscLotteryResult bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.issueNo =:issueNo ");
		parameters.put("issueNo", issueNo);
		List<CqsscLotteryResult> result = super.query(CqsscLotteryResult.class, sql.toString(), parameters);

		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public List<CqsscLotteryResult> findListByIssueDateAndSubNum(String issueDate, Integer startIssueSubNum,
			Integer endIssueSubNum) {
		StringBuilder sql = new StringBuilder(" FROM CqsscLotteryResult bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.issueDate =:issueDate ");
		parameters.put("issueDate", issueDate);

		sql.append(" AND bean.issueSubNum >=:startIssueSubNum ");
		sql.append(" AND bean.issueSubNum <=:endIssueSubNum ");
		parameters.put("startIssueSubNum", startIssueSubNum);
		parameters.put("endIssueSubNum", endIssueSubNum);

		return super.query(CqsscLotteryResult.class, sql.toString(), parameters);
	}

	@Override
	public List<CqsscLotteryResult> findByIssueDate(String startIssueDate, String endIssueDate) {
		StringBuilder sql = new StringBuilder(" FROM CqsscLotteryResult bean WHERE 1=1");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.issueDate >=:startIssueDate ");
		parameters.put("startIssueDate", startIssueDate);
		sql.append(" AND bean.issueDate <=:endIssueDate ");
		parameters.put("endIssueDate", endIssueDate);

		sql.append(" ORDER BY bean.issueNo ASC");

		return super.query(CqsscLotteryResult.class, sql.toString(), parameters);
	}

}
